package controller;

import java.io.IOException;
import java.util.ArrayList;


import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.FacilityDTO;

import bizlogic.FacilityManager;


public class ProcessFacilityServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProcessFacilityServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}
	private void processRequest(HttpServletRequest request, HttpServletResponse response) {
		FacilityManager fm = new FacilityManager();
		FacilityDTO facility = new FacilityDTO();
		facility.setName(request.getParameter("name"));
		facility.setDescription(request.getParameter("description"));
		facility.setType(request.getParameter("type"));
		facility.setSeatingCapacity(Integer.parseInt(request.getParameter("seatingCapacity")));
		System.out.println(request.getParameter("status"));
		facility.setStatus(Integer.parseInt(request.getParameter("status")));
		String ins = (String) request.getParameter("ins");
		if (ins.equalsIgnoreCase("true")) {
			fm.insertFacility(facility);
		} else {
			fm.updateFacility(facility);
		}
		
		try {
			response.sendRedirect("LoadFacility");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		/*ArrayList<FacilityDTO> data = fm.getAll();
		request.setAttribute("facilitys", data);
		RequestDispatcher rd = request.getRequestDispatcher("ShowFacility.jsp");
		try {
			rd.forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		
	}
}
